<template>
	<view v-if="isShow" class="dingyueBox">
		<!-- #ifdef MP-KUAISHOU -->
			<image @click="dingyueKS" class="dingyueBtn" src="https://res.qiguoread.com/staticResources/fellow_townsman/image/uni/ksMessage.gif"></image>
		<!-- #endif -->
		<!-- #ifdef MP-TOUTIAO -->
		<image v-if="wardContent" @click="copyTT" class="copyBtn" src="https://res.qiguoread.com/staticResources/fellow_townsman/image/uni/toutiao/jinqun/icon_qunfudai.png"></image>
		<image @click="dingyueTT" class="dingyueBtn" src="https://res.qiguoread.com/staticResources/fellow_townsman/image/uni/toutiao/ttMessage.gif"></image>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		name: 'navTab',
		props: {

		},
		data() {
			return {
				wardContent:'',
				isShow:false
			};
		},
		mounted() {
			//#ifdef MP-KUAISHOU
				if(getApp().globalData.extConfig['subscribeId4'] || getApp().globalData.extConfig['subscribeId5'] || getApp().globalData.extConfig['subscribeId6']) this.isShow = true;
				else this.isShow = false;
			//#endif
			//#ifdef MP-TOUTIAO
				this.isShow = true;
				this.getIconInfo();
			//#endif
			
		},
		methods: {
			async getIconInfo(){
				let res = await this.getConfigDouyin()
				let useDepartment = 0 // 0:私域运营的 1：短视频运营的
				if (['023009', '023010', '023011', '023012', '023012', '027004', '027005'].includes(getApp().globalData.scene) || ['SHORT_THEATER_PROMOTION_TIKTOK', 'SHORT_THEATER_PROMOTION'].includes(getApp().globalData.spreadModeBatch)) {
					useDepartment = 1
				} else {
					useDepartment = 0
				}
				let wardObj = await this.getWardConfig(0, 0, useDepartment,1);
				console.log('wardObj',wardObj);
				if(wardObj && wardObj.content){
					this.wardContent = wardObj.content;
					this.callBackWardCopy(wardObj.id);
				}
			},
			copyTT(){
				uni.setClipboardData({
					data:this.wardContent,
					success:res=>{
						uni.showToast({
							title:'复制成功！返回/下次进入抖音即可进群领福利哦~',
							icon:'none'
						})
					}
				})
			},
			async dingyueKS() {
			    try {
					if(getApp().globalData.extConfig['subscribeId4'] || getApp().globalData.extConfig['subscribeId5'] || getApp().globalData.extConfig['subscribeId6']){
						let res = await uni.requestSubscribeMessage({
							tmplIds: [getApp().globalData.extConfig['subscribeId4'], getApp().globalData.extConfig['subscribeId5'],getApp().globalData.extConfig['subscribeId6']]
						})
						console.log('dingyueKS',res);
						if((res[0] || {}).errCode === 10001027 || (res[0] || {}).errMsg === 'requestSubscribeMessage:fail repeated request') {
							uni.showToast({
								title: `请去设置开启订阅消息`,
								icon: 'none'
							})
				
							// 弹窗拉取失败
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_fail'
							})
						} else {
							// 弹窗拉取失败
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_exposure'
							})
						}
				
						if((res[1] || {})[getApp().globalData.extConfig['subscribeId4']] === 'ACCEPT') {
							// 弹窗拉取允许
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_agree'
							})
						} else if ((res[1] || {})[getApp().globalData.extConfig['subscribeId4']] === 'REFUSE') {
							// 弹窗拉取拒绝
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_disagree'
							})
						}
				
						this.addDingMess()
					}
			    } catch (error) {
			        console.log(error)
			    }
			},
			// 订阅上报
			async addDingMess() {
				try {
					await this.zwyRequest({
						url: '/mp-api/user_one_time_subscribe/batch',
						method: 'POST',
						data: {
							sourceType: 3,
							sourceId: 0,
							sourceDesc: '',
							templates: [
								{
									templateId: getApp().globalData.extConfig['subscribeId6'],
									templateType: 0
								},
								{
									templateId: getApp().globalData.extConfig['subscribeId4'],
									templateType: 1
								},
								{
									templateId: getApp().globalData.extConfig['subscribeId5'],
									templateType: 2
								}
							]
						}
					})
				} catch (error) {
					console.log(error)
				}
			},
			// 订阅上报
			async addDingMessTT(template) {
				try {
					let res = await this.zwyRequest({
						url: '/mp-api/user_one_time_subscribe/batch',
						method: 'POST',
						data: {
							sourceType: 3,
							sourceId: 0,
							sourceDesc: '',
							templates: template
						}
					})
					console.log('dasdddddd=====', res)
				} catch (error) {
					console.log(error)
				}
			},
			async dingyueTT(){
				try {
					let appId = uni.getStorageSync('appId') || '';
					if(appId){
						let config = uni.getStorageSync('ttConfigMess')
						// let idsObj = {
						// 	'tt8032f68f1f0ba85501': [
						// 		'MSG12794566fe8f56c3dce95c82f7fab466a3e327a15885', // 签到提醒
						// 		'MSG127945641c9befe1cbe0be04995d9fb7f9bd99315721', // 剧集更新提醒
						// 		'MSG1279456d8f91eb701102666783f5525f53793e915722' // 热播新剧提醒
						// 	],
						// 	'tt58602eddc7597c0c01': [
						// 		'MSG12804916fe8f56c3dce95c82f7fab466a3e327a15885',
						// 		'MSG128049141c9befe1cbe0be04995d9fb7f9bd99315721',
						// 		'MSG1280491d8f91eb701102666783f5525f53793e915722'
						// 	],
						// 	'ttce10b8ad512e3bd801': [
						// 		'MSG13333276fe8f56c3dce95c82f7fab466a3e327a15885',
						// 		'MSG133332741c9befe1cbe0be04995d9fb7f9bd99315721',
						// 		'MSG1333327d8f91eb701102666783f5525f53793e915722'
						// 	]
						// }
						// let tmplIdsList = idsObj[appId];
						let tmplIdsList = config && config.templateIds ? config.templateIds : []
						console.log('tmplIdsList---------', tmplIdsList)
						let dingyueRes = await uni.requestSubscribeMessage({
							tmplIds: tmplIdsList
						})
						console.log('dingyueRes',dingyueRes);

						// 订阅消息曝光
						this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
							name: 'Ume_Subscribe_exposure'
						})
						let template = [];
						// 友盟上报
						if(dingyueRes[1] && dingyueRes[1].errMsg === 'requestSubscribeMessage:ok') {
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_agree'
							})
						} else {
							this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
								name: 'Ume_Subscribe_disagree'
							})
						}

						if(dingyueRes[1] && dingyueRes[1][tmplIdsList[0]] === 'accept') { // 剧集更新类型
							template.push({
								templateId: tmplIdsList[0],
								templateType: 0
							})

							
						}
						if(dingyueRes[1] && dingyueRes[1][tmplIdsList[1]] === 'accept') { // 热播新剧提醒类型 
							template.push({
								templateId: tmplIdsList[1],
								templateType: 1
							})
						}
						if(dingyueRes[1] && dingyueRes[1][tmplIdsList[2]] === 'accept') { // 热播新剧提醒类型
							template.push({
								templateId: tmplIdsList[2],
								templateType: 2
							})
						}
						console.log('template',template);
						if(template.length > 0) this.addDingMessTT(template);
					}	
				} catch (error) {
					this.$uma && this.$uma.trackEvent('Ume_homepage_Subscribe', {
						name: 'Ume_Subscribe_fail'
					})
					console.log(error)
				}
				
			}
		}
	}
</script>

<style scoped lang="scss">
	.copyBtn{
		width:118rpx;
		margin-bottom: 20rpx;
		height:125rpx;
	}
	.dingyueBtn{
		width: 146rpx;
		height: 161rpx;
	}
	.dingyueBox{
		position: fixed;
		width: 146rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		// height: 161rpx;
		right: 17rpx;
		bottom: 100rpx;
	}
</style>
